﻿using System;
using System.Collections.Generic;
using System.Text;

namespace PIM_ATE
{
    class KnockRunner
    {
        MircoComHelper m_comObj;
        public string m_sComName;
        bool m_isConnected;
        const string lf = "\r\n";

        public string cmd = "";
        public string result = "";

        public KnockRunner(string ComName)
        {
            m_comObj = new MircoComHelper();
            m_isConnected = false;
            m_sComName = ComName;
        }

        ~KnockRunner()
        {
            if (m_isConnected)
            {
                m_comObj.Close();
            }
        }

        public bool connect()
        {
            if (!m_isConnected)
            {
                m_isConnected = m_comObj.Connect(m_sComName, 9600, System.IO.Ports.Parity.Even, 7);

            }
            return m_isConnected;
        }

        public void close()
        {
            if (m_isConnected)
            {
                m_comObj.Close();
                m_isConnected = false;
            }
        }

        public void set_startNum(int num)
        {
            if (num > 5) num = 5;
            if (num < 1) num = 1;

            byte[] buf = new byte[15];
            buf[0] = 0x02;

            buf[1] = 0x31;

            buf[2] = 0x31;
            buf[3] = 0x31;
            buf[4] = 0x30;
            buf[5] = 0x30;

            buf[6] = 0x30;
            buf[7] = 0x32;

            buf[8] = 0x30;
            buf[9] = (byte)num.ToString()[0];
            buf[10] = 0x30;
            buf[11] = 0x30;

            buf[12] = 0x03;

            buf[13] = 0x31;
            buf[14] = 0x44;

            int chk = 0;
            for (int i = 1; i < buf.Length - 2; i++)
            {
                chk += buf[i];
            }

            string chk_hex = Convert.ToString(chk, 16).ToUpper();
            buf[13] = (byte)chk_hex[chk_hex.Length - 2];
            buf[14] = (byte)chk_hex[chk_hex.Length - 1];

            m_comObj.StartWrite(buf, true);
        }

        public void start_knock(bool isOpen)
        {
            //if (isOpen)
            //    cmd = "7" + "6E08" + Convert.ToChar(3);
            //else
            //    cmd = "8" + "6E08" + Convert.ToChar(3);

            //int chk = 0;
            //for (int i = 0; i < cmd.Length; i++)
            //{
            //    chk += Convert.ToInt16(cmd[i]);
            //}
            //string sChk = Convert.ToString(chk, 16);

            //cmd += sChk.Substring(sChk.Length - 2); ;
            //cmd = Convert.ToChar(2) + cmd;

            //byte[] buf = Encoding.ASCII.GetBytes(cmd);

            byte[] buf = null;
            if (isOpen)
                buf = start_cmd();
            else
                buf = stop_cmd();

            m_comObj.StartWrite(buf, true);
        }

        byte[] start_cmd()
        {
            byte[] buf = new byte[9];
            buf[0] = 0x02;
            buf[1] = 0x37;
            buf[2] = 0x36;
            buf[3] = 0x45;
            buf[4] = 0x30;
            buf[5] = 0x38;
            buf[6] = 0x03;
            buf[7] = 0x31;
            buf[8] = 0x44;
            return buf;
        }

        byte[] stop_cmd()
        {
            byte[] buf = new byte[9];
            buf[0] = 0x02;
            buf[1] = 0x38;
            buf[2] = 0x36;
            buf[3] = 0x45;
            buf[4] = 0x30;
            buf[5] = 0x38;
            buf[6] = 0x03;
            buf[7] = 0x31;
            buf[8] = 0x45;
            return buf;
        }
    }
}
